package edu.szu.huhanpeng.doodlingFight.controller.dataController.gameStart 
{
	import edu.szu.huhanpeng.doodlingFight.interfaces.IChessBoardData;
	import edu.szu.huhanpeng.doodlingFight.interfaces.IChessData;
	import edu.szu.huhanpeng.doodlingFight.interfaces.IChessFriend;
	import edu.szu.huhanpeng.doodlingFight.interfaces.ISquareData;
	import edu.szu.huhanpeng.doodlingFight.model.ChessBoardProxy;
	import edu.szu.huhanpeng.doodlingFight.model.ChessGroupProxy;
	import edu.szu.huhanpeng.doodlingFight.model.data.ChessData;
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.interfaces.INotifier;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	/**
	 * 找出可以被消除的组合
	 * @author Huhanpeng
	 */
	public class HrFindGroupDc extends SimpleCommand implements ICommand, INotifier 
	{
		
		override public function execute(note:INotification):void 
		{
			var chessBoardProxy:ChessBoardProxy = facade.retrieveProxy(ChessBoardProxy.NAME) as ChessBoardProxy;
			var allSquare:Vector.<ISquareData> = chessBoardProxy.allSquare;
			var groupProxy:ChessGroupProxy = facade.retrieveProxy(ChessGroupProxy.NAME) as ChessGroupProxy;
			groupProxy.resetPrepare();
			
			var square:ISquareData;
			var chessData:IChessData;
			var friend:Vector.<IChessData>;
			//以下为方格的编号，数字表方位，5为本体
			//1  2  3
			//4  5  6
			//7  8  9
			var index1:int;
			var index2:int;
			var index3:int;
			var index4:int;
			var index5:int;
			var index6:int;
			var index7:int;
			var index8:int;
			var index9:int;
			for (var i:int = 0; i < allSquare.length; i++) 
			{
				square = allSquare[i]
				chessData = square.chessData;
				friend = chessData.friend;

				
				
			
			
				index5 = square.index;
				index2 = index5 - chessBoardProxy.size;
				index8 = index5 + chessBoardProxy.size;
				index4 = index5 - 1;
				index6 = index5 + 1;
				index1 = index2 - 1;
				index3 = index2 + 1;
				index7 = index8 - 1;
				index9 = index8 + 1;
				
				//检查情况汇总：
				//1: 3 6 7 8
				//2: 7 9        以及棋子下方两格是否同类
				//3: 4 8 9
				//4: 9		    以及棋子右方两格是否同类
				//6: 7			以及棋子左方两格是否同类
				//7: 9
				//8:			以及棋子上方两格是否同类
				//符合以上情况即为准备组，推入“组数据”的“目录”
				if (friend[ChessData.ONE] != null) 
				{
					if (friend[ChessData.THREE] != null) 
					{
						groupProxy.addPrepare( chessData, chessBoardProxy.getChessByIndex(index2), friend[ChessData.ONE], friend[ChessData.THREE]);
					}
					
					if (friend[ChessData.SIX] != null) 
					{
						groupProxy.addPrepare(friend[ChessData.ONE], chessBoardProxy.getChessByIndex(index4), chessData, friend[ChessData.SIX]);
					}
					
					if (friend[ChessData.SEVEN] != null)
					{
						groupProxy.addPrepare(chessData, chessBoardProxy.getChessByIndex(index4), friend[ChessData.ONE], friend[ChessData.SEVEN]);
					}
					
					if (friend[ChessData.EIGHT] != null) 
					{
						groupProxy.addPrepare(friend[ChessData.ONE], chessBoardProxy.getChessByIndex(index2), chessData, friend[ChessData.EIGHT]);
					}
				}
				
				
				if (friend[ChessData.TWO] != null) 
				{
					if (friend[ChessData.SEVEN] != null) 
					{
						groupProxy.addPrepare(friend[ChessData.SEVEN], chessBoardProxy.getChessByIndex(index8 ), chessData, friend[ChessData.TWO]);
					}
					
					if (friend[ChessData.NINE] != null) 
					{
						groupProxy.addPrepare(friend[ChessData.NINE], chessBoardProxy.getChessByIndex(index8), friend[ChessData.TWO], chessData);
					}
					
					//friend属性只涉及周围，相隔一个方格的情况要独立处理
					if (square.downSquare != null && square.downSquare.downSquare != null && square.downSquare.downSquare.chessData.type == chessData.type) 
					{
						groupProxy.addPrepare(chessBoardProxy.getChessByIndex(square.downSquare.downSquare.index), chessBoardProxy.getChessByIndex(square.downSquare.index), chessData, friend[ChessData.TWO]);
					}
				}
				
				
				if (friend[ChessData.THREE] != null) 
				{
					if (friend[ChessData.FOUR] != null) 
					{
						groupProxy.addPrepare(friend[ChessData.THREE], chessBoardProxy.getChessByIndex(index6), friend[ChessData.FOUR], chessData);
					}
					
					if (friend[ChessData.EIGHT] != null) 
					{
						groupProxy.addPrepare(friend[ChessData.THREE], chessBoardProxy.getChessByIndex(index2), chessData, friend[ChessData.EIGHT]);
					}
					
					if (friend[ChessData.NINE] != null) 
					{
						groupProxy.addPrepare(chessData, chessBoardProxy.getChessByIndex(index6), friend[ChessData.THREE], friend[ChessData.NINE]);
					}
				}
				
				
				if (friend[ChessData.FOUR] != null) 
				{
					if (friend[ChessData.NINE] != null) 
					{
						groupProxy.addPrepare(friend[ChessData.NINE], chessBoardProxy.getChessByIndex(index6), chessData, friend[ChessData.FOUR]);
					}
					
					//friend属性只涉及周围，相隔一个方格的情况要独立处理
					if (square.rightSquare != null && square.rightSquare.rightSquare != null && square.rightSquare.rightSquare.chessData.type == chessData.type) 
					{
						groupProxy.addPrepare(chessBoardProxy.getChessByIndex(square.rightSquare.rightSquare.index), chessBoardProxy.getChessByIndex(square.rightSquare.index), chessData, friend[ChessData.FOUR]);
					}
				}
				
				
				if (friend[ChessData.SIX] != null) 
				{
					if (friend[ChessData.SEVEN] != null) 
					{
						groupProxy.addPrepare(friend[ChessData.SEVEN], chessBoardProxy.getChessByIndex(index4), chessData, friend[ChessData.SIX]);
					}
					
					//friend属性只涉及周围，相隔一个方格的情况要独立处理
					if (square.leftSquare != null && square.leftSquare.leftSquare != null && square.leftSquare.leftSquare.chessData.type == chessData.type) 
					{
						groupProxy.addPrepare(chessBoardProxy.getChessByIndex(square.leftSquare.leftSquare.index), chessBoardProxy.getChessByIndex(square.leftSquare.index), chessData, friend[ChessData.SIX]);
					}
				}
				
				
				if (friend[ChessData.SEVEN] != null) 
				{
					if (friend[ChessData.NINE] != null) 
					{
						groupProxy.addPrepare(chessData, chessBoardProxy.getChessByIndex(index8), friend[ChessData.SEVEN], friend[ChessData.NINE]);
					}
				}
				
				
				if (friend[ChessData.EIGHT] != null) 
				{
					//friend属性只涉及周围，相隔一个方格的情况要独立处理
					if (square.upSquare != null && square.upSquare.upSquare != null && square.upSquare.upSquare.chessData.type == chessData.type) 
					{
						groupProxy.addPrepare(chessBoardProxy.getChessByIndex(square.upSquare.upSquare.index), chessBoardProxy.getChessByIndex(square.upSquare.index), chessData, friend[ChessData.EIGHT]);
					}
				}
				
			}
		}
		
		
	}

}